package com.jinlejia.bbs.logic;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.bbs.bean.CommentDetailsBean;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.InformationBean;
import com.jinlejia.bbs.bean.base.SoundBean;
import com.jinlejia.bbs.http.entity.CommentsEntity;
import com.jinlejia.bbs.http.entity.base.PageEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static com.jinlejia.bbs.http.utils.HttpUtils.JsonParse;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;
/**
 * @author 王进 on 2019/4/5.
 */

public class CommentsByAnswerLogic {
    private Context context;
    private LogicCallBack callBack;
    private ArrayList<CommentDetailsBean> dataList;
    public CommentsByAnswerLogic(Context context,ArrayList<CommentDetailsBean> dataList,LogicCallBack callBack){
        this.context = context;
        this.dataList = dataList;
        this.callBack = callBack;
    }

    private int pageIndex;

    public void refresh(String answerCode){
        pageIndex = 1;
        loadData(pageIndex,answerCode);
    }

    public void load(String answerCode){
        pageIndex = pageIndex+1;
        loadData(pageIndex,answerCode);
    }

    private void loadData(final int pageIndex, String answerCode){
        JSONObject json = new JSONObject();
        json.put("answerCode",answerCode);
        json.put("pageIndex",pageIndex);
        json.put("pageSize",20);
        httpCallForm(BBSConfig.listCommentsByAnswer,json)
                .execute(httpCallBack(context, new HttpCallBack() {
                    @Override
                    public void onError(Exception e) {
                        callBack.onResult(false);
                    }

                    @Override
                    public void onResponse(JsonParse parse) {
                        TypeReference<PageEntity<CommentsEntity>> reference = new TypeReference<PageEntity<CommentsEntity>>() {};
                        PageEntity<CommentsEntity> response = parse.parsePageEntity(reference);
                        List<CommentsEntity> data = response.getData();
                        ArrayList<CommentDetailsBean> list = new ArrayList<>();
                        for (CommentsEntity entity:data) {
                            CommentDetailsBean bean = new CommentDetailsBean();
                            Date date = entity.getCreate();
                            SimpleDateFormat format = new SimpleDateFormat("MM-dd hh:mm", Locale.CHINA);
                            String createTime = format.format(date);
                            bean.setCreateTime(createTime);
                            //评论者
                            BBSUserBean commentUser = new BBSUserBean();
                            commentUser.setUserPin(entity.getPin());
                            commentUser.setUserName(entity.getName());
                            commentUser.setUserAvatar(BBSConfig.headerUrl(entity.getUserImageUrl()));
                            commentUser.setUserDuty(entity.getUserTitleLevel());
                            commentUser.setUserLevel(entity.getUserLevel());
                            bean.setCommentUser(commentUser);
                            //评论内容
                            InformationBean commentInfo = new InformationBean();
                            int commentsType = entity.getCommentsType();
                            if (commentsType==1){
                                commentInfo.setMessageContent(entity.getCommentsContent());
                            }else if (commentsType==2){
                                commentInfo.setSoundContent(SoundBean.create(entity.getCommentsContent()));
                            }
                            commentInfo.setCommentsCode(entity.getCommentsCode());
                            commentInfo.setOwn(entity.getIsUser()==1);
                            bean.setCommentInfo(commentInfo);
                            //被评论者
                            BBSUserBean commentedUser = new BBSUserBean();
                            commentedUser.setUserName(entity.getToName());
                            commentedUser.setUserPin(entity.getToPin());
                            bean.setCommentedUser(commentedUser);
                            //被评论内容
                            InformationBean commentedInfo = new InformationBean();
                            bean.setCommentedInfo(commentedInfo);

                            list.add(bean);
                        }
                        if (pageIndex==1){
                            dataList.clear();
                        }
                        dataList.addAll(list);
                        boolean pageAvailable = response.getNextPageAvailable();
                        callBack.onResult(pageAvailable);
                    }

                    @Override
                    public void onNoResponse(int code) {
                        callBack.onResult(false);
                    }
                }));
    }

    public interface LogicCallBack{
        void onResult(boolean hasNextPage);
    }
}
